<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/global/global-head.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>吉车惠后台管理系统</title>
    <%@include file="/global/global-other.jsp" %>
    <script type="text/javascript">
        var $roleGrid;
        var $tree;
        var $cxtx_role = new Object();

        $(function(){
            $roleGrid = $('#role_grid');
            $roleGrid.datagrid({
                fit: true,
                border: false,
                showHeader: false,
                toolbar: [{
                    text: '<i class="fa fa-check-square"></i>授权',
                    handler: function(){
                        $cxtx_role.doGrant();
                    }
                }, {
                    text: '<i class="fa fa-minus-square"></i>解除',
                    handler: function(){
                        $cxtx_role.unGrant();
                    }
                }]
            });
            $tree = $('<ul/>');
            $('#role_grid').data().datagrid.dc.body2.append($tree);


            var rid = "${roleId}";
            //权限
            $cxtx_role.tree = function(){
                $tree.html("");
                $tree.tree({
                    url: '${ctx}/sys/role/allmenus?&r='+Math.random(),
                    checkbox: true,
                    lines: true,
                    onLoadSuccess : function(node, data) {
                        $(this).tree("collapseAll");
                        $.post('${ctx}/sys/role/getgrant?&r='+Math.random(), {roleId: rid} , function(result) {
                            if (result) {
                                for (var i = 0; i < result.length; i++) {
                                    var node = $tree.tree('find', result[i].id);
                                    if (node) {
                                        var isLeaf = $tree.tree('isLeaf', node.target);
                                        if (isLeaf) {
                                            $tree.tree('check', node.target);
                                        }
                                    }
                                }
                            }
                        }, 'json');
                    }
                });
            };

            $cxtx_role.tree();

            //授权
            $cxtx_role.doGrant = function() {
                var checked = $tree.tree('getChecked');
                if (checked && checked.length > 0) {
                    parent.$.messager.confirm('提示', '确定要重新授权吗？', function (flag) {
                        if (flag) {
                            var indeterminate = $tree.tree('getChecked', 'indeterminate');
                            var checkData = $.merge(checked, indeterminate);
                            var gids = [];
                            $.each(checkData, function() {
                                if (this.id) {
                                    gids.push(this.id);
                                }
                            });

                            parent.$.messager.progress({
                            	text : '正在处理中....'
                            });
                            $.post('${ctx}/sys/role/dogrant', {roleId: rid, powerIds: gids.join(',')}, function(result) {
                                if (statusMsg(result)) {
                                    parent.$.messager.progress('close');
                                    parent.$.messager.alert("提示","授权成功！","info");
                                }
                            }, 'JSON');
                        }
                    });
                }else{
                    parent.$.messager.alert("提示","请选择要授权的内容！","error");
                }
            };

            //取消授权
            $cxtx_role.unGrant = function() {
                var checked = $tree.tree('getChecked');
                if (checked && checked.length > 0) {
                    parent.$.messager.confirm('提示', '确定要解除所有授权吗？', function (flag) {
                        if (flag) {
                            $.post('${ctx}/sys/role/ungrant', {roleId: rid}, function(result) {
                                if (statusMsg(result)) {
                                    parent.$.messager.alert("提示","解除成功！","info");
                                    $cxtx_role.tree();
                                }
                            }, 'JSON');
                        }
                    });
                }
            };

        })

    </script>
</head>
<body>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
        <table id="role_grid" >

        </table>
    </div>
</div>
</body>
</html>
